package com.softeem.xyblogmaster.tenant;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.softeem.xyblogmaster.jwt.JwtUtil;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.stereotype.Component;
import com.softeem.xyblogmaster.util.ThreadContext;

@Component
public class CustomTenantHandler implements TenantLineHandler {
    private final JwtUtil jwtUtil;

    public CustomTenantHandler(JwtUtil jwtUtil) {
        this.jwtUtil = jwtUtil;
    }

    @Override
    public Expression getTenantId() {
        //这里，我们后期会从token中获取uid
        String token = ThreadContext.getItem();
        //从tokem中获取uid
        Long tenantId = jwtUtil.getUid(token);
        return new LongValue(tenantId);
    }

    @Override
    public String getTenantIdColumn() {
        return "author_id";
    }

    /**
     * 根据表名判断是否忽略拼接多租户条件
     * 默认都要进行解析并拼接多租户条件
     *
     * @param tableName 表名
     * @return 是否忽略, true:表示忽略，false:需要解析并拼接多租户条件
     */
    @Override
    public boolean ignoreTable(String tableName) {
        if(tableName.equals("blog_articles")){
            return false;
        }
        // 根据需要返回是否忽略该表
        return true;
    }
}
